<template>
  <a-modal
    title="換表"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <template slot="footer">
        <a-button key="back" @click="handleCancel">
          取消
        </a-button>
        <a-button key="submit" type="primary"  @click="handleSubmit">
          保存抄錶
        </a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-row :gutter="24">
        <a-col :md="24" :sm="24">
          <a-table
            size="middle"
            :columns="columns"
            :dataSource="dataMeter"
            :pagination="false"
            :rowKey="(record) => record.id"
            :loading="memberLoading">
            <template slot="reading" slot-scope="text, record">
              <a-input-number
                style="width:100%"
                :precision="2"
                :min="0"
                :value="text"
                :max="99999999"
                @change="e => handleChange(e,record.id, 'reading')"
                :step="10"/>
              </template>
              <template slot="newCost" slot-scope="text, record">
              <a-input-number
                style="width:100%"
                :precision="2"
                :min="0"
                :value="text"
                :max="99999999"
                @change="e => handlenEWChange(e,record.id, 'newCost')"
                :step="10"/>
              </template>
            </a-table>
        </a-col>
      </a-row>
    </a-spin>
  </a-modal>
</template>

<script>
  import moment from 'moment'
  import { getMeterListByStateIdOnJzcb, hydropowerMeterhuanAdd } from '@/api/modular/main/hydropowermeter/hydropowerMeterManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        meterReadingDateString: '',
        visible: false,
        confirmLoading: false,
        memberLoading: false,
        columns: [
          {
            title: '表類型',
            dataIndex: 'typeName',
            width: '20%',
            scopedSlots: { customRender: 'typeName' }
          },
          {
            title: '上期讀數',
            dataIndex: 'preReading',
            width: '20%',
            scopedSlots: { customRender: 'preReading' }
          },
          {
            title: '本次讀數',
            dataIndex: 'reading',
            width: '15%',
            scopedSlots: { customRender: 'reading' }
          },
          {
            title: '用量',
            dataIndex: 'dosage',
            width: '15%',
            scopedSlots: { customRender: 'dosage' }
          },
          {
            title: '費用',
            dataIndex: 'cost',
            width: '15%',
            scopedSlots: { customRender: 'cost' }
          },
          {
            title: '新表底数',
            dataIndex: 'newCost',
            width: '15%',
            scopedSlots: { customRender: 'newCost' }
          }
        ],
        dataMeter: [],
        data: {}
      }
    },
    methods: {
      moment,
      // 初始化方法
      edit (recode) {
        this.data = recode
        this.visible = true
        this.getSdFyListByDetail(recode.estateId, recode.type)
      },
      getSdFyListByDetail(estateId, type) {
        getMeterListByStateIdOnJzcb(estateId, type).then((res) => {
              if (res.code === 200) {
                this.dataMeter = res.data
                this.confirmLoading = false
              } else {
                this.$message.error('查詢失敗')
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
      },
      /**
       * 选择子表单单项触发
       */
      handleChange (value, key, column) {
        const newData = [...this.dataMeter]
        const target = newData.find(item => key === item.id)
        if (target) {
          target[column] = value
          const preReading = target['preReading']
          if (value < preReading) {
            target[column] = preReading
          }
          if (target[column] - preReading < target['solder']) {
            target['dosage'] = target['solder']
          } else {
            target['dosage'] = target[column] - preReading
          }
          target['cost'] = target['dosage'] * target['amount']
          this.dataMeter = newData
        }
      },
      handlenEWChange (value, key, column) {
        const newData = [...this.dataMeter]
        const target = newData.find(item => key === item.id)
        if (target) {
          target[column] = value
          this.dataMeter = newData
        }
      },
      handleSubmit () {
        this.confirmLoading = true
        if (this.dataMeter.length === 0) {
          this.confirmLoading = false
          return false
        }
        hydropowerMeterhuanAdd({ estateId: this.data.estateId, meterResList: this.dataMeter }).then((res) => {
              if (res.code === 200) {
                this.$message.success('換表成功！')
                this.confirmLoading = false
                this.handleCancel()
              } else {
                this.$message.error(res.msg)//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
        })
      },
      onChangerentedDay(date, dateString) {
        this.meterReadingDateString = dateString
      },
      handleCancel () {
        this.data = {}
        this.dataMeter = []
        this.visible = false
      }
    }
  }
</script>
